package com.al.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		//用输入流读取客户端传来的数据
//		InputStream inputStream = req.getInputStream();
//		Reader reader = new InputStreamReader(inputStream);
//		BufferedReader bufferedReader = new BufferedReader(reader);
//		//通过输出流将文件保存到本地硬盘
//		//获取文件夹的绝对路径
//		String path = req.getServletContext().getRealPath("file/copy.txt");
//		OutputStream outputStream = new FileOutputStream(path);
//		Writer writer = new OutputStreamWriter(outputStream);
//		BufferedWriter bufferedWriter = new BufferedWriter(writer);
//		String str = "";
//		while((str = bufferedReader.readLine()) != null) {
//			bufferedWriter.write(str);
//			bufferedWriter.write('\n');
//		}
//		//一定要记得关闭资源
//		bufferedWriter.close();
//		writer.close();
//		outputStream.close();
//		bufferedReader.close();
//		reader.close();
//		inputStream.close();
		
		try {
			DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
			ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
			List<FileItem> list = servletFileUpload.parseRequest(req);
			for(FileItem fileItem : list) {
				//拿到的有可能是个框，有可能是一个文件，所以要加以判断
				if(fileItem.isFormField()) {
					//说明是个框
					String name = fileItem.getFieldName();
					String value = fileItem.getString("UTF-8");//以UTF-8的形式去取他，有可能传汉字
					System.out.println(name+":"+value);
				} else {
					//说明拿到的是文件
					String fileName = fileItem.getName();
					long size = fileItem.getSize();
					//输出一下
					System.out.println(fileName+":"+size+"Byte");
					//存起来
					InputStream inputStream = fileItem.getInputStream();
					BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
					//获取绝对路径，（加上文件名)
					String path = req.getServletContext().getRealPath("file/"+fileName);
					OutputStream outputStream = new FileOutputStream(path);
					BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
					int temp = 0;
					while((temp = bufferedInputStream.read()) != -1) {
						bufferedOutputStream.write(temp);
					}
					bufferedOutputStream.flush();
					bufferedOutputStream.close();
					outputStream.close();
					bufferedInputStream.close();
					inputStream.close();
					System.out.println("上传成功！");
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
